package com.yc.favorite.mapper;

import com.yc.favorite.po.Tag;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface TagMapper {

    @Insert("insert into tag values(default,#{tname},1)")
    @Options(useGeneratedKeys = true, keyColumn = "tid", keyProperty = "tid")
    int insert(Tag tag);

    /**
     * 更新指定的 tag 数量 + 1
     * @param tag
     */
    @Update("update tag set tcount=tcount+1 where tid=#{tid}")
    int addCountByTid(Integer tid);

    @Update("update tag set tcount=tcount+1 where tname=#{tname}")
    int addCountByTname(String tname);

    @Select("select * from tag")
    @Results(id="rm1", value={
            @Result(
                    column = "tid",
                    property = "flist",
                    many = @Many(select =
                            "com.yc.favorite.mapper.FavoriteMapper.selectByTid",
                            fetchType = FetchType.EAGER)
            )
    })
    List<Tag> selectAll();

    @Select("select * from tag where tname=#{tname}")
    Tag selectByTanme(String tname);

}
